package com.smartwebx.ha.biz.rainwater.real.impl;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.google.common.collect.Iterables;
import com.smartwebx.ha.biz.rainwater.feature.FeatureRsvrManager;
import com.smartwebx.ha.biz.rainwater.real.ProcessDataManager;
import com.smartwebx.ha.biz.rainwater.real.RsvrManager;
import com.smartwebx.ha.dal.feature.dao.FeatureRsvrMapper;
import com.smartwebx.ha.dal.realtime.dao.RsvrMapper;
import com.smartwebx.ha.dal.realtime.dao.handler.ProcessDataEnum;
import com.smartwebx.ha.dal.usebean.DateValue;
import com.smartwebx.ha.dal.usebean.FeatureHighLow;
import com.smartwebx.ha.dal.usebean.RsvrFlood;
import com.smartwebx.ha.dal.usebean.WaterYearComp;
import com.smartwebx.ha.dal.usebean.view.ViewDayProcessAnalysis;
import com.smartwebx.ha.dal.util.core.WaterProcessDatasAnalysis;

@Service
public class RsvrManagerImpl implements RsvrManager {
	@Autowired
	private RsvrMapper rsvrDao;
	@Autowired
	private FeatureRsvrMapper featureRsvrDao;
	@Autowired
	private ProcessDataManager processDataService;
	@Autowired
	private FeatureRsvrManager featureRsvrService;

	@Override
	public List<DateValue> getSingleStatWaterDayProcess(String stcd,
			Date startDate, Date endDate) {
		return WaterProcessDatasAnalysis.getSingleStatDayProcess(rsvrDao, stcd,
				startDate, endDate, "water");
	}

	@Override
	public List<DateValue> getSingleStatWaterHourProcess(String stcd,
			Date startHour, Date endHour) {

		return WaterProcessDatasAnalysis.getSingleStatHourProcess(rsvrDao,
				stcd, startHour, endHour, "water");
	}

	@Override
	public List<DateValue> getPurenessWaterProcesss(String stcd,
			Date startDate, Date endDate) {
		return WaterProcessDatasAnalysis.getPurenessProcesss(rsvrDao, stcd,
				startDate, endDate, "water");
	}

	@Override
	public WaterYearComp getMoreYearWaterCompSotr(String stcd, Date startDate,
			Date endDate) {
		Iterable<DateValue> maxMinAvgIter = processDataService
				.getWaterProcessMaxMinAvg(startDate, endDate,
						ProcessDataEnum.SHUI_KU, stcd);
		return WaterProcessDatasAnalysis.getMoreYearCompSort(featureRsvrDao,
				stcd, maxMinAvgIter, "water");
	}

	@Override
	public List<DateValue> getSingleStatFlowDayProcess(String stcd,
			Date startDate, Date endDate) {
		return WaterProcessDatasAnalysis.getSingleStatDayProcess(rsvrDao, stcd,
				startDate, endDate, "flow");
	}

	@Override
	public List<DateValue> getSingleStatFlowHourProcess(String stcd,
			Date startHour, Date endHour) {
		return WaterProcessDatasAnalysis.getSingleStatHourProcess(rsvrDao,
				stcd, startHour, endHour, "flow");
	}

	@Override
	public List<DateValue> getPurenessFlowProcesss(String stcd, Date startDate,
			Date endDate) {
		return WaterProcessDatasAnalysis.getPurenessProcesss(rsvrDao, stcd,
				startDate, endDate, "flow");
	}

	@Override
	public WaterYearComp getMoreYearFlowCompSotr(String stcd, Date startDate,
			Date endDate) {
		Iterable<DateValue> maxMinAvgIter = processDataService
				.getFlowProcessMaxMinAvg(startDate, endDate,
						ProcessDataEnum.SHUI_KU, stcd);
		return WaterProcessDatasAnalysis.getMoreYearCompSort(featureRsvrDao,
				stcd, maxMinAvgIter, "flow");
	}

	@Override
	public RsvrFlood getRsvrFlood(String stcd) {
		return rsvrDao.getRsvrFlood(stcd);
	}

	@Override
	public ViewDayProcessAnalysis getDayProcessAnalysis(String stcd, Date bTm,
			Date eTm) {
		FeatureHighLow featureHighLow = featureRsvrService
				.getMoreYearStatHighLowWater(stcd);// 历史最高最低水位
		Iterable<DateValue> waterMaxMinAvg = processDataService
				.getWaterProcessMaxMinAvg(bTm, eTm, ProcessDataEnum.SHUI_KU,
						stcd);// 水位最高最低平均
		int exceedWarnDay = processDataService.getReservoirExceedWarnDays(bTm,
				eTm, stcd);// 超警天数
		// 水位多年历史比较
		WaterYearComp waterYearComp = this.getMoreYearWaterCompSotr(
				stcd, bTm, eTm);
		ViewDayProcessAnalysis viewDayProcessAnal = new ViewDayProcessAnalysis();
		// 历史最高水位
		viewDayProcessAnal.setHisMaxWater(new DateValue(featureHighLow
				.getMaxzTm(), featureHighLow.getMaxz()));
		// 历史最低水位
		viewDayProcessAnal.setHisMinWater(new DateValue(featureHighLow
				.getMinzTm(), featureHighLow.getMinz()));
		// 最高水位
		viewDayProcessAnal.setMaxWater(Iterables.get(waterMaxMinAvg, 0));
		// 最低水位
		viewDayProcessAnal.setMinWater(Iterables.get(waterMaxMinAvg, 1));
		// 最高水位排序
		viewDayProcessAnal.setMaxWaterSort(waterYearComp.getMaxSotrHistory());
		// 最低水位排序
		viewDayProcessAnal.setMinWaterSort(waterYearComp.getMinSotrHistory());
		// 平均水位
		viewDayProcessAnal.setCurAvgWater(Iterables.get(waterMaxMinAvg, 2));
		// 历史平均水位
		viewDayProcessAnal.setHisAvgWater(waterYearComp.getAvgHisValue());
		// 平均水位排序
		viewDayProcessAnal.setAvgWaterSort(waterYearComp.getAvgSotrHistory());
		// 警戒天数
		viewDayProcessAnal.setWarDayNum(exceedWarnDay);
		// 水位最大值序列
		viewDayProcessAnal.setWaterMaxs(waterYearComp.getMaxHisArray());
		// 水位最小值序列
		viewDayProcessAnal.setWaterMins(waterYearComp.getMinHisArray());
		viewDayProcessAnal.setWaterAvgs(waterYearComp.getAvgHisArray());
		return viewDayProcessAnal;
	}

}
